﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Slja.DBUtility;//Please add references
namespace Slja.DAL
{
	/// <summary>
	/// 数据访问类:LinkMan
	/// </summary>
	public partial class LinkManDAL
	{
        public LinkManDAL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "biz_LinkMan"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from biz_LinkMan");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Slja.Model.LinkManModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into biz_LinkMan(");
            strSql.Append("FullName,pinyin,ShortPY,Type,Gender,Birthday,MobileTel,JobTel,JobAddress,JobPosition,Email,Company,Memo)");
			strSql.Append(" values (");
            strSql.Append("@FullName,@pinyin,@ShortPY,@Type,@Gender,@Birthday,@MobileTel,@JobTel,@JobAddress,@JobPosition,@Email,@Company,@Memo)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@FullName", SqlDbType.NVarChar,20),
					new SqlParameter("@pinyin", SqlDbType.NVarChar,40),
					new SqlParameter("@ShortPY", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.NVarChar,5),
					new SqlParameter("@Gender", SqlDbType.TinyInt,1),
					new SqlParameter("@Birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@MobileTel", SqlDbType.NVarChar,30),
					new SqlParameter("@JobTel", SqlDbType.NVarChar,50),
					new SqlParameter("@JobAddress", SqlDbType.NVarChar,60),
					new SqlParameter("@JobPosition", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Company", SqlDbType.NVarChar,30),
                    new SqlParameter("@Memo",SqlDbType.NVarChar,-1)
                                        };
			parameters[0].Value = model.FullName;
			parameters[1].Value = model.Pinyin;
			parameters[2].Value = model.ShortPY;
			parameters[3].Value = model.Type;
			parameters[4].Value = model.Gender;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.MobileTel;
			parameters[7].Value = model.JobTel;
			parameters[8].Value = model.JobAddress;
			parameters[9].Value = model.JobPosition;
			parameters[10].Value = model.Email;
			parameters[11].Value = model.Company;
            parameters[12].Value = model.Memo;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Slja.Model.LinkManModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update biz_LinkMan set ");
			strSql.Append("FullName=@FullName,");
			strSql.Append("pinyin=@pinyin,");
			strSql.Append("ShortPY=@ShortPY,");
			strSql.Append("Type=@Type,");
			strSql.Append("Gender=@Gender,");
			strSql.Append("Birthday=@Birthday,");
			strSql.Append("MobileTel=@MobileTel,");
			strSql.Append("JobTel=@JobTel,");
			strSql.Append("JobAddress=@JobAddress,");
			strSql.Append("JobPosition=@JobPosition,");
			strSql.Append("Email=@Email,");
			strSql.Append("Company=@Company,");
            strSql.Append("Memo=@Memo");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@FullName", SqlDbType.NVarChar,20),
					new SqlParameter("@pinyin", SqlDbType.NVarChar,40),
					new SqlParameter("@ShortPY", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.NVarChar,5),
					new SqlParameter("@Gender", SqlDbType.TinyInt,1),
					new SqlParameter("@Birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@MobileTel", SqlDbType.NVarChar,30),
					new SqlParameter("@JobTel", SqlDbType.NVarChar,50),
					new SqlParameter("@JobAddress", SqlDbType.NVarChar,60),
					new SqlParameter("@JobPosition", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Company", SqlDbType.NVarChar,30),
                     new SqlParameter("@Memo",SqlDbType.NVarChar,-1),
					new SqlParameter("@ID", SqlDbType.Int,4)                   
                                        };
			parameters[0].Value = model.FullName;
			parameters[1].Value = model.Pinyin;
			parameters[2].Value = model.ShortPY;
			parameters[3].Value = model.Type;
			parameters[4].Value = model.Gender;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.MobileTel;
			parameters[7].Value = model.JobTel;
			parameters[8].Value = model.JobAddress;
			parameters[9].Value = model.JobPosition;
			parameters[10].Value = model.Email;
			parameters[11].Value = model.Company;
            parameters[12].Value = model.Memo;
			parameters[13].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from biz_LinkMan ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from biz_LinkMan ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Slja.Model.LinkManModel GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 ID,FullName,pinyin,ShortPY,Type,Gender,Birthday,MobileTel,JobTel,JobAddress,JobPosition,Email,Company,Memo from biz_LinkMan ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			Slja.Model.LinkManModel model=new Slja.Model.LinkManModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
                AssignModel(ds.Tables[0].Rows[0], model);
				return model;
			}
			else
			{
				return null;
			}
		}
        public void AssignModel(DataRow dr ,Slja.Model.LinkManModel model)
        {
            if (dr["TableName"] != null && dr["TableName"].ToString() != "")
            {
                model.Tablename = dr["TableName"].ToString();
            }
            if (dr["ID"] != null && dr["ID"].ToString() != "")
            {
                model.ID = int.Parse(dr["ID"].ToString());
            }
            if (dr["FullName"] != null && dr["FullName"].ToString() != "")
            {
                model.FullName = dr["FullName"].ToString();
            }
            if (dr["pinyin"] != null && dr["pinyin"].ToString() != "")
            {
                model.Pinyin = dr["pinyin"].ToString();
            }
            if (dr["ShortPY"] != null && dr["ShortPY"].ToString() != "")
            {
                model.ShortPY = dr["ShortPY"].ToString();
            }
            if (dr["Type"] != null && dr["Type"].ToString() != "")
            {
                model.Type = dr["Type"].ToString();
            }
            if (dr["Gender"] != null && dr["Gender"].ToString() != "")
            {
                model.Gender = int.Parse(dr["Gender"].ToString());
            }
            if (dr["Birthday"] != null && dr["Birthday"].ToString() != "")
            {
                model.Birthday = DateTime.Parse(dr["Birthday"].ToString());
            }
            if (dr["MobileTel"] != null && dr["MobileTel"].ToString() != "")
            {
                model.MobileTel = dr["MobileTel"].ToString();
            }
            if (dr["JobTel"] != null && dr["JobTel"].ToString() != "")
            {
                model.JobTel = dr["JobTel"].ToString();
            }
            if (dr["JobAddress"] != null && dr["JobAddress"].ToString() != "")
            {
                model.JobAddress = dr["JobAddress"].ToString();
            }
            if (dr["JobPosition"] != null && dr["JobPosition"].ToString() != "")
            {
                model.JobPosition = dr["JobPosition"].ToString();
            }
            if (dr["Email"] != null && dr["Email"].ToString() != "")
            {
                model.Email = dr["Email"].ToString();
            }
            if (dr["Company"] != null && dr["Company"].ToString() != "")
            {
                model.Company = dr["Company"].ToString();
            }
            if (dr["Memo"] != null && dr["Memo"].ToString() != "")
            {
                model.Memo = dr["Memo"].ToString();
            }
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(string strWhere, string orderBy, int pageSize,int curPageindex, out int recordCount)
		{
            string sql = GetSQL(strWhere, true);
			//return DbHelperSQL.Query(strSql.ToString());
            return DAL.Common.DALUti.GetPageData(sql, orderBy, pageSize, curPageindex, out recordCount);
		}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string sql = GetSQL(strWhere, false);
            return DbHelperSQL.Query(sql);            
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, bool IsContainSysUser)
        {
            string sql = GetSQL(strWhere, IsContainSysUser);
            return DbHelperSQL.Query(sql);
        }
        string GetSQL(string strWhere,bool IsContainSysUser)
        {
            StringBuilder strSql = new StringBuilder();
            if (!IsContainSysUser)
            {

                strSql.Append("select ID,FullName,pinyin,ShortPY,Type,Gender,Birthday,MobileTel,JobTel,JobAddress,JobPosition,Email,(select ClientName from dbo.biz_Client c where c.ClientID=m.company ) as company,Memo ");
                strSql.Append(" FROM biz_LinkMan m");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
            }
            else
            {
                strSql.Append(@"
                        select TableName,ID,FullName,pinyin,ShortPY,[Type],Gender,Birthday,MobileTel,JobTel,JobAddress,JobPosition,Email,company,Memo
                        from (
                            select 'biz_LinkMan' as TableName, 
                            ID,FullName,pinyin,ShortPY,[Type],Gender,Birthday,MobileTel,JobTel,JobAddress,JobPosition,Email,(select ClientName from dbo.biz_Client c where c.ClientID=m.company ) as company,Memo FROM biz_LinkMan m 
                            union all
                            select 'sys_User',UserID,U_Cname,U_UserNo,U_EName,0,U_sex,U_BirthDay,U_MobileNo,U_Extension,'',(select v_Text from dbo.sys_FieldValue f where f.ValueID=u.U_Title),U_companyMail,'思朗佳安',U_Remark
                            from dbo.sys_User u where u.U_status<>2 and u.UserID<>1) t");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
            }
            return strSql.ToString();
        }

		#endregion  Method
	}
}

